package com.ruoyi.workflow.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.workflow.mapper.BusStoreInfoMapper;
import com.ruoyi.workflow.domain.BusStoreInfo;
import com.ruoyi.workflow.service.IBusStoreInfoService;

/**
 * 门店信息Service业务层处理
 *
 * @author xiaodeng
 * @date 2022-11-24
 */
@Service
public class BusStoreInfoServiceImpl extends ServiceImpl<BusStoreInfoMapper, BusStoreInfo> implements IBusStoreInfoService {

    @Autowired
    private ISysUserService userService;

    /**
     * 查询门店信息列表
     *
     * @param busStoreInfo 门店信息
     * @return 门店信息
     */
    @Override
    public List<BusStoreInfo> selectBusStoreInfoList(BusStoreInfo busStoreInfo) {
        return getBaseMapper().selectBusStoreInfoList(busStoreInfo);
    }

    @Override
    public boolean save(BusStoreInfo entity) {
        if (entity.getStatus() == 2) {
            throw new ServiceException("新增时不允许将状态选择为已删除!!!");
        }
        Date date = new Date();
        entity.setCreatedTime(date);
        SysUser user = userService.selectUserById(entity.getManagerId());
        if (user == null) {
            throw new ServiceException("操作异常");
        }
        entity.setManagerName(user.getNickName());
        entity.setManagerTel(user.getPhonenumber());
        SysUser userNew = SecurityUtils.getLoginUser().getUser();
        entity.setCreatorId(userNew.getUserId());
        return super.save(entity);
    }

    @Override
    public boolean updateById(BusStoreInfo entity) {
        if (entity.getStatus() == 2) {
            throw new ServiceException("更新时不允许将状态修改为已删除!!!");
        }
        Date date = new Date();
        entity.setUpdatedTime(date);
        SysUser userNew = SecurityUtils.getLoginUser().getUser();
        entity.setCreatorId(userNew.getUserId());
        return super.updateById(entity);
    }

    @Override
    public boolean removeBatchById(Long[] ids) {
        if (ids.length == 0) {
            return false;
        }
        return getBaseMapper().removeBatchById(ids);
    }
}
